<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/jsp/common/common.jsp"%>

<html>
<script language="javascript">

			Ext.onReady(function(){
				var tb = new Ext.Toolbar({
			   		renderTo: document.all.tbar,
			    	items: [
				        {
				            text: '上传',
				            icon:"<%=contextPath%>/images/up.png",
				            handler:function () {
				            	upload();
				            }
				        },'-',{
				            text: '清空',
				            icon:"<%=contextPath%>/images/clear.png",
				            handler:function () {
				            	document.getElementById("frmFile").reset();
				            }
				        },'-', 
				        {
				            text: '关闭',
				            icon:"<%=contextPath%>/images/banjie.png",
				            handler:function () {
				            	parent.window.close();
				            }
				        }
				        
			    	]
			    });
			});

			String.prototype.endWith=function(str){
				if(str==null||str==""||this.length==0||str.length>this.length)
				return false;
				if(this.substring(this.length-str.length)==str)
					return true;
				else
					return false;
				return true;
			}
	
	// 上传附件
	function upload(){

		var imageflag = <%=session.getAttribute("imageflag")%>;

		if (document.getElementById("uploadFile").value == "") {
			alert("系统检测到您的提交有以下错误：\t\t\t\t\n 1:文件必须选择。");
			return false;
		} else if (imageflag =="1" && !document.getElementById("uploadFile").value.endWith("jpg")
				&& !document.getElementById("uploadFile").value.endWith("JPG")
				&& !document.getElementById("uploadFile").value.endWith("PNG")
				&& !document.getElementById("uploadFile").value.endWith("png")
				&& !document.getElementById("uploadFile").value.endWith("bmp")
				&& !document.getElementById("uploadFile").value.endWith("BMP")
				&& !document.getElementById("uploadFile").value.endWith("gif")
				&& !document.getElementById("uploadFile").value.endWith("GIF")
				){
			alert("系统检测到您的提交有以下错误：\t\t\t\t\n 1:图片必须选择以jpg、JPG、PNG、png、BMP、bmp、GIF、gif结尾的文件。");
			return false;
		}
		
		var frm = document.getElementById("frmFile");
		frm.action = "<%=contextPath%>/file!upload.do";//+ window.opener.document.getElementById("sb.id").value;
		frm.submit();
		
	}

	// 浏览图片
	function openImage(id,name) {
		   document.getElementById("result").innerHTML = "";
		   var frm = document.getElementById("frmFile");
		   var str = "<%=contextPath%>/jsp/common/image.jsp?id=" + id;
  		   parent.addTab(name,str);

	}
	
	// 下载文件
	function downloadFile(id) {
		  document.getElementById("result").innerHTML = "";
		  document.getElementById("download").src = "<%=contextPath%>/file!fileDownload.do?id="+ id;
	}
	
	// 删除文件
    function deleteFile(id,trid){
    
    	document.getElementById("result").innerHTML = "";
    
    	if (confirm("确定要删除文件?")) {
    	
				Ext.Ajax.request( {
					method :'POST',
					url :'file!fileDelete.do?id='+ id,
					success : function(response) {
        				var mytable = document.getElementById("mybody");  
        				var myrow = document.getElementById(trid);   
       				    mytable.deleteRow(myrow.rowIndex-1);  
						
					},
					failure : function() {
						alert("删除失败");
					}
				});
		}
    }

</script>
	<body>
		<div id ='tbar' ></div>
		<center><div id="result"><font color="red">${info}</font></div></center>
		<form id="frmFile" action="/file!upload.do" method="post" enctype="multipart/form-data">
			<table width="100%" id="contab">
				<tr>
					<th width="10%">
						选择文件：
					</th>
					<td width="90%">
						<s:file name="uploadFile" contentEditable="false" size="70" />
							&nbsp;&nbsp;&nbsp;
						
					</td>					
				</tr>
			
				<c:if test="${imageflag=='1'}">	
					<tr>
						<td align="left" width="100%"colspan="2"><img src="<%=contextPath%>/images/redlight.gif"/><font color="red">&nbsp;&nbsp;附件最大1M。
						<br><img src="<%=contextPath%>/images/redlight.gif"/>&nbsp;&nbsp;格式为JPG、jpg、PNG、png、BMP、bmp、GIF、gif 图片。</font>
					</td>
					</tr>
				</c:if>
				<c:if test="${imageflag=='0'}">	
					<tr>
						<td align="left" width="100%"colspan="2"><img src="<%=contextPath%>/images/redlight.gif"/><font color="red">&nbsp;&nbsp;附件最大1M。</font></td>
					</tr>
				</c:if>
			</table>

			<br>
			
			<table width="100%" id="listtab">
				<tr>
					<th class="ct" width="60%">文件名称</th>
					<th class="ct" width="20%" align="center">下载（<img src="<%=contextPath%>/images/diqu.png" >浏览）</th>
					<th class="ct" width="10%" align="center" >删除</th>
				</tr>
				 <tbody id="mybody">
				<c:forEach items="${fileList}" var="file" varStatus="status">
					<tr id='tr${status.index}'>
						<!--td><script language="javascript">document.write(parseInt('${status.index}') + parseInt(1))</script></td-->
						<td class="lf"><c:out value="${file.filename}"/><br></td>
						<td class="ct">
								<c:if test="${file.imageflag=='1'}">
									&nbsp;&nbsp;&nbsp;
								</c:if>
								<img style='cursor:pointer' alt="下载文件" onclick="downloadFile('${file.id}')" src="<%=contextPath%>/images/save.gif" >
								&nbsp;
								<c:if test="${file.imageflag=='1'}">
									<img style='cursor:pointer' alt="浏览图片" onclick="openImage('${file.id}','${file.filename}')" src="<%=contextPath%>/images/diqu.png" >
								</c:if>
						<br></td>
						<td class="ct">
							<%
								if (session.getAttribute("btn_" + session.getAttribute("colname").toString() + "_display") != null 
								|| "1".equals(session.getAttribute("SESSION_ISADMIN"))) {
							%>
								<img style='cursor:pointer' alt="删除文件" onclick="deleteFile('${file.id}','tr${status.index}')" src="<%=contextPath%>/images/delete.gif" ><br>
							<%
								}
							%>
							
						</td>
					</tr>
				</c:forEach>
				</tbody>
			</table>
			<iframe id="download" style="display:none"></iframe>
		</form>
	</body>
</html>